package es.udc.fic.hamNeggs.manager;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import es.udc.fic.hamNeggs.pojo.UserStoriesComment;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Repository(value = "userStoriesComments")
public class UserStoriesCommentManager extends AbstractManager {
    private final static Logger LOG = Logger.getLogger(UserStoriesCommentManager.class.getName());

    @Transactional(readOnly = true)
    @SuppressWarnings("unchecked")
    public List<UserStoriesComment> getCommentList() {
        List<UserStoriesComment> list = session.createQuery("from UserStoriesComments").list();
        Collections.sort(list,
                new Comparator<UserStoriesComment>() {
                    public int compare(UserStoriesComment o1, UserStoriesComment o2) {
                        return o1.getId().intValue() - o2.getId().intValue();
                    }
                });
        return list;
    }

    @Transactional(readOnly = true)
    public UserStoriesComment getUserStoriesComment(Long id) {
        return (UserStoriesComment) session.get(UserStoriesComment.class, id);
    }

    @Transactional(readOnly = false)
    public void saveUserStoriesComment(UserStoriesComment comment) {
        super.save(comment, LOG);
    }

    @Transactional(readOnly = false)
    public void deleteUserStoriesComment(UserStoriesComment comment) {
        super.delete(comment, LOG);
    }

    @Transactional(readOnly = false)
    public void updateUserStoriesComment(UserStoriesComment comment) {
        super.update(comment, LOG);
    }

}
